$(function(){
	
	$(".uploadattch").click(function(){
		$('.file').trigger('click');
	})
	$(".file").on("change",function(){
		var filePath=$(this).val();
		var arr=filePath.split('\\');
        var fileName=arr[arr.length-1];
        $(".filename").text(fileName);
	})
	//删除
	$(".tab-content").on("click",".task-delete",function(){
		var id=$(this).attr("id");
		layer.confirm('删除不可恢复,确定删除？',{closeBtn: 0},function(index){
			refreshPanel("/task/taskdelete?id="+id+"","任务管理");
			 layer.close(index);
		})
	})
	
	$(".return-task").click(function(){
		layer.confirm('正在添加任务,确认退出？',{closeBtn: 0},function(index){
			 refreshPanel("/task/taskmanage","任务管理");
			 layer.close(index);
		})
	})
	$(".st").change(function(){
		 refreshPanel("/task/statustype?status="+$(this).val()+"","任务管理");
	})
	//附件下载
	$(".info-content").on("click",".download",function(){
		var attid=$(this).attr("attid");
		window.open("/mail/download/?attid="+attid+"");
	})
	$(".info-record").on("click",".download",function(){
		var attid=$(this).attr("attid");
		window.open("/mail/download/?attid="+attid+"");
	})
	
	$('.process-update').click(function(){
		$(".file").val("");
		$(".filename").text("");
		var taskid=$(this).attr('taskid');
		var type=$(this).attr('type');
		$(".status").val($(this).attr("status"));
		if($(this).text()=="添加批注"){
			$(".pizhuContent").val("");
			var formdata=new FormData();
			layer.open({ 
				  type: 1,		
				  skin: 'demo-class',
				  skin: 'yourclass',
				  offset: '100px',
				  title :'任务进度和反馈',
				  area: ["450px","260px"], //宽高
				  content:$(".task-pizhu"),
				  closeBtn: 0,
				  btn:['确定','取消'],
				  btnAlign: 'c', 
				  yes:function(index, layero){
						var tfContent=$(".pizhuContent").val();
						formdata.append("file",$(".file")[0].files[0]);
						formdata.append("tfContent",tfContent);
						formdata.append("taskid",taskid);
					  $.ajax({
						  url:"/task/savepizhu",
						  type:"post",
						  async:false,
						  cache:false,
						  processData:false,
						  contentType:false,
						  data:formdata,
						  success:function(){
						  }
					  })
					  layer.close(index);
					 $(".refresh-this").trigger("click");
				  },
				  btn2: function(index, layero){
					layer.close(index);		   
				  }
			})	
		}else{
			updatestatus(450,350,$(".process-update-table"),taskid,type,".tfContent");
		}
	})
		/**
	 * 提交部门表单
	 */
	$("#taskForm").form({
		url: "/task/savetask" ,
		onSubmit:function(){
			//任务名称
			if($("input[name=tmTitle]").val()==""){
				layer.tips('<span style="color:#aa0000">'+'任务名称不能为空！'+'</span>', $("input[name=tmTitle]"), {
					tips : [ 2, 'rgb(236, 228, 221)' ]
				});
				return false;
			}
			//任务开始时间
			if($(".start-time").val()==""){
				layer.tips('<span style="color:#aa0000">'+'任务时间不能为空'+'</span>', $("input[name=tmStartTime]"), {
					tips : [ 2, 'rgb(236, 228, 221)' ]
				});
				return false;
			}	
			//任务结束时间
			if($(".end-time").val()==""){
				layer.tips('<span style="color:#aa0000">'+'任务时间不能为空'+'</span>', $("input[name=tmEndTime]"), {
					tips : [ 2, 'rgb(236, 228, 221)' ]
				});
				return false;
			}	
		},
		success:function(data){
			if(data){
				layer.msg('保存成功',{shift: -1,time:1000}, function(){
					refreshPanel(getUrlPrefix("/task/taskmanage"),"任务管理");
				});
			}else{
				layer.msg('保存失败',{shift: -1,time:1000});
			}
	    }
	});
	
	//查找任务
	$(".taskmanage-search").click(function(){
		var value=$(".key").val();
		if(value==""){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>查询不能为空！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}
		refreshPanel(getUrlPrefix("/task/taskmanage?value="+value+""),"任务管理");
	})
	//查找我的任务
	$(".search-mytask").click(function(){
		var value=$(".key").val();
		if(value==""){
			var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>查询不能为空！！！</span>\<\/div>";	 
			promptbox(obj);	
			return;
		}
		refreshPanel(getUrlPrefix("/task/mytask?value="+value+""),"任务管理");
	})
	//添加联系人
	$(".fancybox").click(function() {
		layer.open({
			type : 1,
			skin : 'demo-class',
			skin : 'yourclass',
			offset : '100px',
			title : '联系人',
			area : [ '804px', '492px' ], // 宽高
			content : $(".contacts-tables"),
			closeBtn : 0,
			btn : [ '确定', '取消' ],
			btnAlign : 'c',
			yes : function(index, layero) {
				$(".getmain-user").val("");
				$(".userid-input").val("");
				var obj=$(".icheckbox_flat-green-checked");
				obj.each(function(){				
					$(".getmain-user").val($(".getmain-user").val()+"、"+$(this).attr("name"));
					$(".userid-input").val($(".userid-input").val()+"、"+$(this).attr("data"));
				})
					var str=$(".getmain-user").val();
					var newstr=str.substr(1,str.length);				
					var idstr=$(".userid-input").val();
					var newidstr=idstr.substr(1,idstr.length);
					
					$(".getmain-user").val(" "+newstr);
					$(".userid-input").val(newidstr);
					layer.close(index);
			},
			btn2 : function(index, layero) {
				layer.close(index);
			}
		});
	})
	
	/**
	 * 报表模糊查寻
	 */
	$('.tast-check').on('click','.search-btn',function(){
		refreshPanel('/task/check?key='+$('.key').val()+'&did='+$('.dept option:selected').val(),
				"任务报表")
	})
})
//提示框
function promptbox(content){
	  layer.open({ 
		  type: 1,		
		  skin: 'demo-class',
		  skin: 'yourclass',
		  offset: '100px',
		  title :'提示',
		  area: ["300px","200px"], //宽高
		  content: content,
		  closeBtn: 0,
		  btn:['关闭'],
		  btnAlign: 'c', 
		  btn2: function(index, layero){
			layer.close(index);		   
		  }
		})
}
//邮件选中
function select(e){
	if($(e).hasClass("icheckbox_flat-green-checked")){
		$(e).removeClass("icheckbox_flat-green-checked");
	}else{
		$(e).addClass("icheckbox_flat-green-checked")
	}	
}
function updatestatus(width,height,content,taskid,type,classname){
	var formdata=new FormData();
	$(classname).val("");
	layer.open({ 
		  type: 1,		
		  skin: 'demo-class',
		  skin: 'yourclass',
		  offset: '100px',
		  title :'任务进度和反馈',
		  area: [""+width+"px",""+height+"px"], //宽高
		  content:content,
		  closeBtn: 0,
		  btn:['确定','取消'],
		  btnAlign: 'c', 
		  yes:function(index, layero){
				var tfContent=$(classname).val();
				var status=$('.status').val();
				formdata.append("file",$(".file")[0].files[0]);
				formdata.append("tfContent",tfContent);
				formdata.append("status",status);
				formdata.append("taskid",taskid);
				formdata.append("type",type);
			  $.ajax({
				  url:"/task/statusupdate",
				  type:"post",
				  async:false,
				  cache:false,
				  processData:false,
				  contentType:false,
				  data:formdata,
				  success:function(){
				  }
			  })
			  layer.close(index);
			 $(".refresh-this").trigger("click");
		  },
		  btn2: function(index, layero){
			layer.close(index);		   
		  }
	})	

}